package com.lc.viewResolve;

import com.lc.annotation.MyService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Created by lichao on 2017/12/30.
 */
@MyService(value = "defaultViewResolve")
public class DefaultViewResolve implements ViewResolve {

    @Override
    public boolean resolve(HttpServletRequest request, HttpServletResponse response, View view) {
        String viewName =this.getClass().getClassLoader().getResource("/").getPath()+ view.getViewName() + ".html";
        FileReader fileReader ;
        BufferedReader bufferedReader;
        try {
            String line = "";
            fileReader = new FileReader(viewName);
            bufferedReader = new BufferedReader(fileReader);
            while ((line=bufferedReader.readLine())!=null){
                response.getWriter().print(line);
            }
            bufferedReader.close();
            fileReader.close();
        }catch(Exception e){
            System.out.println("文件解析异常~~");
            return false;
        }
        return true;
    }

}
